import java.util.*;

public class Main1 {



    //主持人调度（二）
    public int minmumNumberOfHost(int n,int[][] startEnd){
        Arrays.sort(startEnd,(a,b)->{
           return a[0]-b[0];
        });
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        heap.offer(startEnd[0][1]);
        for (int i = 1; i < n; i++) {
            int a = startEnd[i][0],b = startEnd[i][1];
            if (a>=heap.peek()){
                heap.poll();
                heap.offer(b);
            }else{
                heap.offer(b);
            }
        }
        return heap.size();
    }




    //走迷宫
    public static  int N = 1010;
    public static int[]dx = {0,0,1,-1};
    public static int[]dy = {-1,1,0,0};
    public static  int n,m,x1,x2,y1,y2;
    public static char[][] arr = new char[N][N];
    public static int[][] dist = new int[N][N];

    public static int bfs(){
        if (arr[x2][y2] == '*') return -1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dist[i][j] = -1;
            }
        }
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{x1,y1});
        dist[x1][y1] = 0;

        while(!q.isEmpty()){
            int[] t = q.poll();
            int a =t[0],b = t[1];
            for (int i = 0; i < 4; i++) {
                int x = a+dx[i],y =b+dy[i];
                if(x>=1 && x<= n && y>=1 && y<=m && arr[x][y] == '.' && dist[x][y] == -1){
                    q.add(new int[]{x,y});
                    dist[x][y] = dist[a][b] +1;
                    if(x==x2 && y == y2){
                        return dist[x][y];
                    }
                }
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        x1 = in.nextInt();
        y1 = in.nextInt();
        x2 = in.nextInt();
        y2 = in.nextInt();
        for (int i = 1; i <= n; i++) {
            String tmp = in.next();
            for (int j = 1; j <= m; j++) {
                arr[i][j] = tmp.charAt(j-1);
            }
        }
        System.out.println(bfs());
    }


    //kotori和气球
    public static  int MOD = 109;
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int n= in.nextInt();
        int m = in.nextInt();
        int ret  =n;
        for (int i = 0; i < m-1; i++) {
            ret = ret*(n-1)%MOD;
        }
        System.out.println(ret);
    }

}
